﻿Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.General
Imports DoAnQuanLyBanHang1065043_1065007.DoAnQuanLyBanHang.DAO
Imports DevComponents.DotNetBar

Public Class frmDatHang
    Private m_QuyDinh As New QuyDinh()
    Private m_DatHangCtrl As New DatHangCtrl()
    Private m_MatHangCtrl As New MatHangCtrl()
    Private m_LoaiTienCtrl As New LoaiTienCtrl()
    Private m_LoaiGiaoDichCtrl As New LoaiGiaoDichCtrl()
    Private m_KhachHangCtrl As New KhachHangCtrl()
    Public Sub New()
        InitializeComponent()
        DataService.OpenConnection()
    End Sub
    Private Sub btnThemMH_Click(sender As System.Object, e As System.EventArgs) Handles btnThemMH.Click
        LoadMain.HienThiMatHang()
    End Sub

    Private Sub frmDatHang_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_LoaiTienCtrl.HienThiComboBox(cmbLoaiTienGui)
        m_LoaiTienCtrl.HienThiDataGridViewComboBoxColumn(colLoaiTien)
        m_LoaiGiaoDichCtrl.HienThiComboBox(cmbLoaiGiaoDich)
        m_LoaiGiaoDichCtrl.HienThiDataGridViewComboBoxColumn(colLoaiGD)
        m_KhachHangCtrl.HienThiComboBox(cmbTenKH)
        m_KhachHangCtrl.HienThiDataGridViewComboBoxColumn(colTenKH)
        m_MatHangCtrl.HienThiComboBox(cmbTenMH)
        m_MatHangCtrl.HienThiDataGridViewComboBoxColumn(colTenMH)
        m_DatHangCtrl.HienThi(DGVDatHang, BindingNavigatorDatHang, txtMaPhieu, dtNgayDat, txtSoLuong, txtGiaBan, _
         txtThanhTien, txtGhiChu, cbThanhToan, cmbTenMH, cmbTenKH, cmbLoaiGiaoDich, _
         cmbLoaiTienGui)
    End Sub

    Private Sub bindingNavigatorRefreshItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorRefreshItem.Click
        m_LoaiTienCtrl.HienThiComboBox(cmbLoaiTienGui)
        m_LoaiTienCtrl.HienThiDataGridViewComboBoxColumn(colLoaiTien)
        m_LoaiGiaoDichCtrl.HienThiComboBox(cmbLoaiGiaoDich)
        m_LoaiGiaoDichCtrl.HienThiDataGridViewComboBoxColumn(colLoaiGD)
        m_KhachHangCtrl.HienThiComboBox(cmbTenKH)
        m_KhachHangCtrl.HienThiDataGridViewComboBoxColumn(colTenKH)
        m_MatHangCtrl.HienThiComboBox(cmbTenMH)
        m_MatHangCtrl.HienThiDataGridViewComboBoxColumn(colTenMH)
        m_DatHangCtrl.HienThi(DGVDatHang, BindingNavigatorDatHang, txtMaPhieu, dtNgayDat, txtSoLuong, txtGiaBan, _
         txtThanhTien, txtGhiChu, cbThanhToan, cmbTenMH, cmbTenKH, cmbLoaiGiaoDich, _
         cmbLoaiTienGui)
    End Sub

    Public Function KiemTraTruocKhiLuu(str As [String]) As [Boolean]
        For Each row As DataGridViewRow In DGVDatHang.Rows
            If row.Cells(str).Value IsNot Nothing Then
                Dim m_Str As [String] = row.Cells(str).Value.ToString()
                If m_Str = "" Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    Private Sub bindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        If KiemTraTruocKhiLuu("colMaPhieu") = True AndAlso KiemTraTruocKhiLuu("colTenMH") = True AndAlso KiemTraTruocKhiLuu("colTenKH") = True AndAlso KiemTraTruocKhiLuu("colGiaBan") = True AndAlso KiemTraTruocKhiLuu("colThanhTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiGD") = True AndAlso KiemTraTruocKhiLuu("colNgayDat") = True AndAlso KiemTraTruocKhiLuu("colSoLuong") = True Then
            bindingNavigatorPositionItem.Focus()
            m_DatHangCtrl.LuuDatHang()
        Else
            MessageBoxEx.Show("Giá trị ô không được rỗng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub

    Private Sub bindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorDeleteItem.Click
        If DGVDatHang.RowCount = 0 Then
            bindingNavigatorDeleteItem.Enabled = False

        ElseIf MessageBox.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            BindingNavigatorDatHang.BindingSource.RemoveCurrent()
        End If
    End Sub

    Private Sub bindingNavigatorCloseItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorCloseItem.Click
        Me.Close()
    End Sub

    Private Sub btnThemKH_Click(sender As System.Object, e As System.EventArgs) Handles btnThemKH.Click
        LoadMain.HienThiKhachHang()
    End Sub

    Private Sub btnHuy_Click(sender As System.Object, e As System.EventArgs) Handles btnHuy.Click
        Me.Close()
    End Sub

    Private Sub btnNhapLai_Click(sender As System.Object, e As System.EventArgs) Handles btnNhapLai.Click
        txtGhiChu.Text = ""
        txtGiaBan.Text = "0"
        txtSoLuong.Text = "0"
        txtThanhTien.Text = "0"
        cbThanhToan.Checked = False
    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        If DGVDatHang.RowCount = 0 Then
            BindingNavigatorDatHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_DatHangCtrl.ThemDongMoi()
        m_Row("MaPhieu") = "DH" & m_QuyDinh.LaySTT(DGVDatHang.Rows.Count + 1)
        m_Row("MaMH") = ""
        m_Row("MaKH") = ""
        m_Row("NgayDat") = DateTime.Today
        m_Row("SoLuong") = 0
        m_Row("GiaBan") = 0
        m_Row("ThanhTien") = 0
        m_Row("MaLoaiGiaoDich") = ""
        m_Row("MaLoaiTien") = ""
        m_Row("GhiChu") = ""
        m_Row("ThanhToan") = False

        m_DatHangCtrl.ThemMoi(m_Row)
        BindingNavigatorDatHang.BindingSource.MoveLast()
    End Sub

    Private Sub btnLuu_Click(sender As System.Object, e As System.EventArgs) Handles btnLuu.Click
        If KiemTraTruocKhiLuu("colMaPhieu") = True AndAlso KiemTraTruocKhiLuu("colTenMH") = True AndAlso KiemTraTruocKhiLuu("colTenKH") = True AndAlso KiemTraTruocKhiLuu("colGiaBan") = True AndAlso KiemTraTruocKhiLuu("colThanhTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiTien") = True AndAlso KiemTraTruocKhiLuu("colLoaiGD") = True AndAlso KiemTraTruocKhiLuu("colNgayDat") = True AndAlso KiemTraTruocKhiLuu("colSoLuong") = True Then
            bindingNavigatorPositionItem.Focus()
            m_DatHangCtrl.LuuDatHang()
            MessageBoxEx.Show("Đã lưu thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBoxEx.Show("Phải nhập đầy đủ dữ liệu để thêm đơn đặt hàng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub

    Private Sub btnSua_Click(sender As System.Object, e As System.EventArgs) Handles btnSua.Click
        Dim thanhtoan As Boolean = False
        If cbThanhToan.Checked = True Then
            thanhtoan = True
        Else
            thanhtoan = False
        End If
        m_DatHangCtrl.SuaDatHang(txtMaPhieu.Text, cmbTenMH.SelectedValue.ToString(), cmbTenKH.SelectedValue.ToString(), Convert.ToInt64(txtSoLuong.Text), Convert.ToInt64(txtGiaBan.Text), Convert.ToInt64(txtThanhTien.Text), _
         dtNgayDat.Value, thanhtoan, cmbLoaiTienGui.SelectedValue.ToString(), cmbLoaiGiaoDich.SelectedValue.ToString(), txtGhiChu.Text)
        m_DatHangCtrl.HienThi(DGVDatHang, BindingNavigatorDatHang, txtMaPhieu, dtNgayDat, txtSoLuong, txtGiaBan, _
         txtThanhTien, txtGhiChu, cbThanhToan, cmbTenMH, cmbTenKH, cmbLoaiGiaoDich, _
         cmbLoaiTienGui)
        MessageBoxEx.Show("Đã sửa thành công!", "Hoàn Thành", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnDel_Click(sender As System.Object, e As System.EventArgs) Handles btnDel.Click
        If MessageBoxEx.Show("Bạn có chắc chắn xóa đặt hàng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            m_DatHangCtrl.XoaDatHang(txtMaPhieu.Text, cmbTenMH.SelectedValue.ToString(), cmbTenKH.SelectedValue.ToString())
            m_DatHangCtrl.HienThi(DGVDatHang, BindingNavigatorDatHang, txtMaPhieu, dtNgayDat, txtSoLuong, txtGiaBan, _
             txtThanhTien, txtGhiChu, cbThanhToan, cmbTenMH, cmbTenKH, cmbLoaiGiaoDich, _
             cmbLoaiTienGui)
        End If
    End Sub

    Private Sub bindingNavigatorAddNewItem_Click(sender As System.Object, e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
        If DGVDatHang.RowCount = 0 Then
            BindingNavigatorDatHang.Enabled = True
        End If
        Dim m_Row As DataRow = m_DatHangCtrl.ThemDongMoi()
        m_Row("MaPhieu") = "DH" & m_QuyDinh.LaySTT(DGVDatHang.Rows.Count + 1)
        m_Row("MaMH") = ""
        m_Row("MaKH") = ""
        m_Row("NgayDat") = DateTime.Today
        m_Row("SoLuong") = 0
        m_Row("GiaBan") = 0
        m_Row("ThanhTien") = 0
        m_Row("MaLoaiGiaoDich") = ""
        m_Row("MaLoaiTien") = ""
        m_Row("GhiChu") = ""
        m_Row("ThanhToan") = False

        m_DatHangCtrl.ThemMoi(m_Row)
        BindingNavigatorDatHang.BindingSource.MoveLast()
    End Sub
End Class